<template>
<div>
  <el-card shadow="never">
    <el-form :inline="true">
      <el-form-item label="订单状态">
        <el-select v-model="status" clearable placeholder="请选择订单状态">
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-button type="warning" @click="loadOrderList">查询</el-button>
    </el-form>
  </el-card>
  <el-table ref="multipleTable" v-loading="loading" :data="tableData" tooltip-effect="dark"
      border style="width: 100%" max-height="450px" :header-cell-style="tableHeaderCellStyle">
    <el-table-column prop="id" label="商品编号" width="120" sortable />
    <el-table-column prop="name" label="商品名" width="140" />
    <el-table-column prop="number" label="数量" width="140" sortable />
    <el-table-column prop="price" label="单价" width="140" sortable />
    <el-table-column prop="groupPrice" label="团购价" width="140" sortable />
    <el-table-column prop="total" label="总价" width="140" sortable />
    <el-table-column prop="groupTotal" label="团购总价" width="140" sortable />
    <el-table-column prop="payType" label="支付方式" width="140" />
    <el-table-column prop="phoneNumber" width="140" label="电话" />
    <el-table-column prop="username" width="140" label="收货人" />
    <el-table-column prop="createTime" width="160" label="下单日期" />
    <el-table-column label="状态" width="140">
      <template v-slot="scope">
        {{map[scope.row.status]}}
      </template>
    </el-table-column>
    <el-table-column width="150" fixed="right" label="操作">
      <template v-slot="scope">
        <el-button size="mini" type="warning" @click="updateStatus(scope.$index, scope.row)">修改订单状态
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-dialog title="选择订单状态" :visible.sync="dialogVisible" width="30%"
             :before-close="handleClose" :close-on-click-modal="false">
    <el-radio v-model="radio" :label="0">已支付</el-radio>
    <el-radio v-model="radio" :label="1">已发货</el-radio>
    <el-radio v-model="radio" :label="2">已完成</el-radio>
    <el-radio v-model="radio" :label="3">已退款</el-radio>
    <span slot="footer" class="dialog-footer">
      <el-button type="warning" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
import { loadList,updateStatus } from "@/api/order";

export default {
  name: "Order",
  data() {
    return{
      status: '',
      options: [
        {label:'已支付',value:0},
        {label:'已发货',value:1},
        {label:'已完成',value:2},
        {label:'已退款',value:3}
      ],
      loading: false,
      tableData: [],
      map: ['已支付','已发货','已完成','已退款',],
      dialogVisible: false,
      radio: '',
      selectRow: ''
    }
  },
  mounted() {
    this.loadOrderList()
  },
  methods: {
    tableHeaderCellStyle () {
      return 'background-color: #fcf3ec;'
    },
    loadOrderList() {
      this.loading =true
      loadList({status: this.status}).then(res => {
        this.tableData = res.data
        this.loading = false
      })
    },
    updateStatus(index,row){
      this.selectRow = row
      this.radio = row.status
      this.dialogVisible = true
    },
    handleClose(done) {
      this.selectRow = ''
      this.radio = ''
      done()
    },
    submit(){
      if (this.selectRow.status === this.radio){
        this.dialogVisible = false
      }else {
        let order = {
          id: this.selectRow.id,
          status: this.radio
        }
        updateStatus(order).then(()=>{
          this.$message({
            type: 'success',
            message: '修改成功',
            center: true
          })
          this.selectRow = ''
          this.dialogVisible = false
          this.loadOrderList()
        })
      }
    }
  }
}
</script>

<style scoped>

</style>
